package com.system

import grails.gorm.transactions.Transactional

@Transactional
class RequestMapService {

    def springSecurityService

    def init() {
        def save = { it.save(flush: true) }
        if (User.count() == 0) {

            //用户
            def user1 = save new User("admin", "admin", "342418262@qq.com")
            def user2 = save new User("test", "test", "888888@qq.com")

            //角色
            def role1 = save new Role("ROLE_ADMIN", "超级管理员")
            def role2 = save new Role("ROLE_USER", "普通用户")

            //用户-角色
            UserRole.create(user1, role1, true)
            UserRole.create(user2, role2, true)

            //角色组
            def roleGroup1 = save new RoleGroup("安全组")
            def roleGroup2 = save new RoleGroup("网络组")

            //角色组-角色
            RoleGroupRole.create(roleGroup1, role1, true)
            RoleGroupRole.create(roleGroup1, role1, true)

            //用户-角色组
            UserRoleGroup.create(user1, roleGroup1, true)
            UserRoleGroup.create(user2, roleGroup2, true)

            def anon = [
                    '/**',
                    '/error',
                    '/index',
                    '/index.gsp',
                    '/shutdown',
                    '/logout/**',
                    '/assets/**',
                    '/**/js/**',
                    '/**/css/**',
                    '/**/images/**',
                    '/**/favicon.ico',
                    '/login/**',
                    '/druid/**',
                    '/user/**',
                    '/verifyCode/index'
            ]

            for (String url in anon) {
                if (!RequestMap.findByUrl(url)) {
                    save new RequestMap("permitAll", url)
                }
            }

            //清理缓存
            springSecurityService.clearCachedRequestmaps()
        }
    }
}
